from dailyspents.controller.SpentController import SpentController
from dailyspents.controller.UserController import UserController
from dailyspents.controller.CategoryController import CategoryController
from dailyspents.controller.PaymentController import PaymentController
from dailyspents.controller.LocalController import LocalController
from dailyspents._exceptions import DatabaseException
import cherrypy

class GenericView(object):
    
    def __init__(self):
        self.user = None
        self.categoryController = None
        self.userController = None
        self.paymentController = None
        self.spentController = None
        self.localController = None
        self.message = ''
        self.nameSpace = {}
        
        #self.makeCommonNamespace()
        
    def makeCommonNamespace(self):
        
        self.nameSpace = {'errorMessage': None,
                            'message': None,
                            'showStatistics': False,
                            'offset': 0,
                            'limit': self.user.settings.numberOfShownSpent,
                            'doNavigation': True,
                            'query': '',
                            'id': 0,
                            'totalOfSpents': None}
        return self.nameSpace
    
    def makeContextMenu(self):
        self.nameSpace['contextMenu'] = [('Home', '/main'),
                                ('New spent', '/spent?page=newspent'),
                                ('Filter', '/spent?page=filter')]
        
    def index(self, message=None, errorMessage=None):
        
        self.checkAuthentication()
        
        self.categoryController = CategoryController(self.user)
        self.paymentController = PaymentController(self.user)
        self.spentController = SpentController(self.user)
        self.userController = UserController()
        self.localController = LocalController(self.user)
        
        self.controllers = {'category': self.categoryController,
                            'payment': self.paymentController,
                            'local': self.localController,
                            'user': self.userController,
                            'spent': self.spentController}
        
        self.nameSpace = self.makeCommonNamespace()
        self.nameSpace['message'] = message
        self.nameSpace['errorMessage'] = errorMessage
        
        self.makeContextMenu()
        
    def checkAuthentication(self):
        self.user = cherrypy.session.get('user', None)
        
        if self.user == None:
            raise cherrypy.HTTPRedirect('/index')
        
    #def _cpOnError(self):
    #    cherrypy.response.headerMap['Status'] = '404 Not Found'
    #    cherrypy.response.body = ['''<html>
    #        <head>
    #                <title>Daily Spent</title>
    #                <link rel="stylesheet" href="/static/css/main.css"></link>
    #                <META http-equiv="Refresh" content="0; url=/main">
    #        </head>
    #        
    #        <body>
    #        redirecting...
    #        </body>
    #        
    #        </html>''']